﻿using System;
using System.IO;
using System.Linq;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using Resn.WhuPub.BIL;
using System.Collections.Generic;
using Resn.WhuPub.DAL;
using Resn.WhuPub.DML;

namespace Resn.WhuPub.BLL
{
        /// <summary>
        /// 实现省/直辖市管理器的属性和方法
        /// </summary>
        public class ProvinceManager:IProvinceManager
        {
                #region 属性

                private static ProvinceManager _instance;

                /// <summary>
                /// 获取省/直辖市管理器实例
                /// </summary>
                /// <value>
                /// 省/直辖市管理器实例
                /// </value>
                public static ProvinceManager Instance
                {
                        get { return  _instance??(_instance = new ProvinceManager()); }
                }


                /// <summary>
                /// 获取省/直辖市集合
                /// </summary>
                /// <value>
                /// 省/直辖市集合
                /// </value>
                public List<IProvince> Provinces
                {
                        get
                        {
                                var models = DBStorage.Instance.Select<ProvinceModel>();

                                var provinces = new List<IProvince>();

                                models.ForEach(i => provinces.Add(new Province(i)));

                                return provinces;
                        }
                }

                /// <summary>
                /// 获取省/直辖市集合
                /// </summary>
                /// <value>
                /// 省/直辖市集合
                /// </value>
                public Dictionary<string, List<string>> ProvinceCitys
                {
                        get
                        {
                                var ids = new Dictionary<string, string>();
                                var result = new Dictionary<string, List<string>>();

                                var provinceModels = DBStorage.Instance.Select<ProvinceModel>();
                                foreach (var provinceModel in provinceModels)
                                {
                                        ids.Add(provinceModel.Id, provinceModel.Name);
                                        result.Add(provinceModel.Name, new List<string>());
                                }

                                var cityModels = DBStorage.Instance.Select<CityModel>();
                                foreach (var cityModel in cityModels)
                                {
                                        result[ids[cityModel.ProvinceId]].Add(cityModel.Name);
                                }

                                return result;
                        }
                }
                #endregion

                #region 方法



                /// <summary>
                /// 创建省/直辖市
                /// </summary>
                /// <param name="name">名称</param>
                /// <returns>
                /// 创建的省/直辖市对象
                /// </returns>
                public IProvince CreateProvince(string name)
                {
                        var model = new ProvinceModel
                        {
                                Id = Guid.NewGuid().ToString(),
                                Name = name,
                        };

                        return new Province(model);
                }
                #endregion
        }
}